package logobj.btd;

import logobj.config.ConfProperties;
import logobj.dao.LogDao;
import logobj.utils.BigFileUtils;
import logobj.utils.OutLog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AnalysisAction {
    //开始进行文件处理
    public void startAction(Map<String, ArrayList> fineinfomap){

        OutLog.printf("开始进行日志索引建立或进行分割");
        TextFileDispost textFileDispost = new TextFileDispost();
        textFileDispost.fSplit(fineinfomap);
        textFileDispost = null;
        System.gc();
        //统计攻击次数 写入等保单位统计数据
        LogDao logDao = new LogDao();
        logDao.insertDbdwTj(filecount());
        OutLog.printf("开始进行日志分析。");
        AnalysisInsertDB analysisInsertDB = new AnalysisInsertDB();
        analysisInsertDB.analylog(ConfProperties.ftp_localpath_sublog);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if("Y".equals(ConfProperties.file_runafter_del.toUpperCase())){
            BigFileUtils bigFileUtils = new BigFileUtils();
            bigFileUtils.deleteDir(ConfProperties.ftp_localpath);
        }
    }

    /***
     *
     * 统计攻击次数
     * */
    public  Map<String,Long> filecount(){
        //开始进行攻击次数统计
        OutLog.printf("开始进行攻击次数统计。");
        BigFileUtils bigFileUtils = new BigFileUtils();
        ArrayList<String> filenamepath = bigFileUtils.getFileList(ConfProperties.ftp_localpath,null);
        String sublogpath="";
        int pointion = 0;
        String gjdate = "";
        long rowcount = 0;
        Map<String,Long> tjmap = new HashMap<>();
        for(int i =0;i < filenamepath.size(); i++){
            sublogpath = filenamepath.get(i);
            sublogpath = sublogpath.replace("\\","/");
            pointion = sublogpath.replace("\\","/").lastIndexOf("/")+ 1;
            gjdate = sublogpath.substring(pointion, pointion+ 10 );
            rowcount = bigFileUtils.getFastFileConut(filenamepath.get(i));
            if(tjmap.containsKey(gjdate)){
                tjmap.put(gjdate,tjmap.get(gjdate)+ rowcount);
            }else {
                tjmap.put(gjdate,rowcount);
            }
        }
        return tjmap;
    }



}
